<template>
  <component
    is="a-modal"
    v-model:visible="visible"
    :width="800"
    :title="title"
    :mask-closable="false"
    :ok-loading="loading"
    @cancel="close"
    @before-ok="submit">
    <!-- 表单信息 start -->
    <a-form ref="formRef" :model="formData" :rules="rules" :auto-label-width="true">
		<a-form-item label="排序" field="sort_order">
		  <a-input-number v-model="formData.sort_order" placeholder="请输入排序" />
		</a-form-item>
		<a-form-item label=" 供应/需求" field="supply_demand">
		 <a-select
		   v-model="formData.supply_demand"
		   :options="supplyDemandData"
		   allow-clear
		   placeholder="请选择供应/需求"
		 />
		</a-form-item>
		 <a-form-item label="产品分类" field="product_category">
		   <a-select
		     v-model="formData.product_category"
		     :options="productCategoryData"
		     allow-clear
		     placeholder="请选择产品分类"
		   />
		 </a-form-item>
		<a-form-item label="关联企业" field="enterprise_id">
		  <a-select
		    v-model="formData.enterprise_id"
		    :options="enterpriseIdData"
		    allow-clear
		    placeholder="请选择关联企业"
		  />
		</a-form-item>
	<a-form-item label="是否推荐" field="is_recommend">
	  <sa-select v-model="formData.is_recommend" dict='yes_or_no' placeholder="请输入是否推荐" />
	</a-form-item>	
		
	<a-form-item label="商品图片" field="image">
	  <sa-upload-image v-model="formData.image" :limit="1" :multiple="false" />
	</a-form-item>	
      <a-form-item label="商品名称" field="product_name">
        <a-input v-model="formData.product_name" placeholder="请输入商品名称" />
      </a-form-item>
	
      <a-form-item label="商品副标题" field="product_subtitle">
        <a-input v-model="formData.product_subtitle" placeholder="请输入商品副标题" />
      </a-form-item>
     <a-form-item label="商品详情" field="supply_demand_datail">
	 		   <ma-wangEditor v-model="formData.supply_demand_datail" :height="400" placeholder="请输入商品详情" />
	 </a-form-item>
    </a-form>
    <!-- 表单信息 end -->
  </component>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { Message, Modal } from '@arco-design/web-vue'
import api from '../api/demand'
import commonApi from '@/api/common'


const emit = defineEmits(['success'])
// 引用定义
const visible = ref(false)
const loading = ref(false)
const formRef = ref()
const mode = ref('')
const productCategoryData=ref('')
const enterpriseIdData=ref('')
const supplyDemandData=ref('')

let title = computed(() => {
  return '供求列表' + (mode.value == 'add' ? '-新增' : '-编辑')
})

// 表单信息
const formData = reactive({
  sd_id: null,
  sort_order:0,
  lang_code: '',
  product_name: '',
  product_subtitle: '',
  supply_demand: '',
  product_category: '',
  image:'',
  enterprise_id:'',
  supply_demand_datail:'',
  is_recommend:0
})

// 验证规则
const rules = {
  lang_code: [{ required: true, message: '语言代码，如zh-CN、en-US等必需填写' }],
  product_name: [{ required: true, message: '商品名称必需填写' }],
  image:[ {required: true, message: '封面图必需填写' }],
  product_subtitle: [{ required: true, message: '商品副标题必需填写' }],
  supply_demand: [{ required: true, message: '供应/需求必需填写' }],
  product_category:[{required:true,message:'请选择产品分类'}],
  enterprise_id:[{required:true,message:'请选择关联企业'}],
  is_recommend:[{required:true,message:'请选择是否推荐'}],
  supply_demand_datail:[{required:true,message:'请填写商品详情'}]
}

// 打开弹框
const open = async (type = 'add') => {
  mode.value = type
  visible.value = true
  formRef.value.resetFields()
  await initPage()
}

// 初始化页面数据
const initPage = async () => {
	const enterpriseId= await commonApi.commonGet('enterpriseContact/enterpriseContact/getClass')
	enterpriseIdData.value=enterpriseId.data
	const productCategory = await commonApi.commonGet('/cassification/ClassificationDictionary/getCate',{classification_type:1})
	productCategoryData.value = productCategory.data
	const supplyDemand = await commonApi.commonGet('/cassification/ClassificationDictionary/getCate',{classification_type:16})
	supplyDemandData.value = supplyDemand.data
	}

// 设置数据
const setFormData = async (data) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      formData[key] = data[key]
    }
  }
}

// 数据保存
const submit = async (done) => {
  const validate = await formRef.value?.validate()
  if (!validate) {
    loading.value = true
    let data = { ...formData }
    let result = {}
    if (mode.value === 'add') {
      // 添加数据
      data.sd_id = undefined
      result = await api.save(data)
    } else {
      // 修改数据
      result = await api.update(data.sd_id, data)
    }
    if (result.code === 200) {
      Message.success('操作成功')
      emit('success')
      done(true)
    }
    // 防止连续点击提交
    setTimeout(() => {
      loading.value = false
    }, 500)
  }
  done(false)
}

// 关闭弹窗
const close = () => (visible.value = false)

defineExpose({ open, setFormData })
</script>
